<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/24
 * Time: 10:27
 */

namespace app\api\controller;

use app\api\business\ShopCarBus;
use think\facade\Db;

class ShopCar extends UserCommon
{
    public function __construct(){
        parent::__construct();
    }

    /**
     * 加入购物车
     * @return \think\response\Json
     */
    public function add(){
        $data = input('param.','','trim');
        try{
            $id = (new ShopCarBus())->add($data);
            if ($id){
                return resJson(200,'添加成功',['id' => $id]);
            }else{
                return resJson(0,'添加失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }



    /**
     * 获取购物车列表
     * @return \think\response\Json
     */
    public function getList(){
        $where = ['a.user_id' => $this->user_id];
        try{
            $list = (new ShopCarBus())->getList($where);
            return resJson(200,'ok',$list);
        }catch (\Exception $e){
            return resJson(0,$e->getMessage());
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function delete(){
        $ids = input('post.ids','','trim');
        if (!$ids) return resJson(0,'参数错误');
        try{
            (new ShopCarBus())->del($ids);
            return resJson(200,'删除成功');
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 修改购物车的数量
     * @return \think\response\Json
     */
    public function edit(){
        $id = input('param.id',0,'intval');
        $share_num = input('param.share_num',0,'intval');
        if (!$id || !$share_num){
            return resJson(0,'参数错误');
        }
        try{
            $res = (new ShopCarBus())->edit($id,$share_num);
            if ($res){
                return resJson(200,'操作成功');
            }else{
                return resJson(0,'操作失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }



}